package com.zhnc.dangjianpingtaiapp.gui.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.flyco.tablayout.SlidingTabLayout;
import com.wangdh.utilslibrary.utils.logger.TLog;
import com.zhnc.dangjianpingtaiapp.R;
import com.zhnc.dangjianpingtaiapp.gui.DemoFragment;

import java.util.ArrayList;

/**
 * 第一个itme 界面
 */
public class AffairFragment extends Fragment {
    public AffairFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private final String[] mTitles = {
            "党务", "政务", "商务", "社会服务", "村务"
    };

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_affair, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        table = view.findViewById(R.id.table);
        mViewPager = view.findViewById(R.id.viewpager);
        ArrayList<Fragment> mFragments = new ArrayList<>();
        mFragments.add(new HomeDangWuFragment());
        mFragments.add(new HomeZhengWuFragment());
        mFragments.add(new DemoFragment());
        mFragments.add(new HomeSheHuiFragment());
        mFragments.add(new HomeCunWuFragment());
        //设置可以滑动
//        table.setTabMode(TabLayout.MODE_SCROLLABLE);
        table.setTextBold(true);
        setViewPager(mTitles, getChildFragmentManager(), mFragments);
    }

    private ViewPager mViewPager;
    private SlidingTabLayout table;

    public void setViewPager(String[] titles, FragmentManager fa, ArrayList<Fragment> fragments) {
        if (mViewPager == null) {
            throw new IllegalStateException("ViewPager can not be NULL !");
        }

        if (titles == null || titles.length == 0) {
            throw new IllegalStateException("Titles can not be EMPTY !");
        }

        this.mViewPager.setAdapter(new InnerPagerAdapter(fa, fragments, titles));

        this.mViewPager.removeOnPageChangeListener(table);
        this.mViewPager.addOnPageChangeListener(table);
        table.setViewPager(mViewPager, mTitles);
        table.notifyDataSetChanged();
    }

    class InnerPagerAdapter extends FragmentPagerAdapter {
        private ArrayList<Fragment> fragments = new ArrayList<>();
        private String[] titles;

        public InnerPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments, String[] titles) {
            super(fm);
            this.fragments = fragments;
            this.titles = titles;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            // 覆写destroyItem并且空实现,这样每个Fragment中的视图就不会被销毁
            // super.destroyItem(container, position, object);
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
    }
}
